﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace Tank90
{
    internal class LoadMap
    {
        private static Thread LoadMapThread;
        private static List<Map> MapsTank = new List<Map>();
        private bool IsLoad;
        private Graphics pGraphics;

        public void GetMap()
        {
            var dirinfo = new DirectoryInfo(Application.StartupPath);
            string filePath = dirinfo.Parent.Parent.FullName + "\\MapData\\MyMap.xlsx";
            if (MapsTank.Count == 0 && !IsLoad)
            {
                MapsTank = ReadMaps(filePath);
                IsLoad = true;
            }

            //foreach (Map map in MapsTank)
            //{
            //    if (!map.elemenType.Equals(MapElemenType.None))
            //    {
            //        map.Drawing(graphics);
            //    }

            //}
        }


        public List<Map> ReadMaps(string mapFile)
        {
            string filePath = mapFile;

            var maps = new List<Map>();

            IWorkbook hssfworkbook;
            try
            {
                using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    hssfworkbook = new XSSFWorkbook(file);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            ISheet sheet = hssfworkbook.GetSheetAt(0);
            IEnumerator rows = sheet.GetRowEnumerator();
            rows.MoveNext();
            IRow row;

            try
            {
                while (rows.MoveNext())
                {
                    var map = new Map();
                    row = (XSSFRow) rows.Current;
                    map.x = Convert.ToInt32(row.GetCell(2).ToString());
                    map.y = Convert.ToInt32(row.GetCell(3).ToString());
                    map.Type = row.GetCell(1).ToString();
                    //map.Drawing(pGraphics);
                    maps.Add(map);
                }
            }
            catch (Exception e)
            {
            }


            return maps;
        }
    }
}